package com.MedDroid.task;

import android.os.AsyncTask;
import com.MedDroid.MainApp;
import com.MedDroid.activity.AddNewUserActivity;
import com.MedDroid.control.DatabaseControl;
import com.MedDroid.entities.User;

import java.lang.ref.WeakReference;

/**
 * Created with IntelliJ IDEA.
 * User: Romen
 * Date: 02/12/13
 * Time: 4:14 PM
 * Description:
 */
public class AddNewUserTask extends AsyncTask<Void, Void, Boolean>
{
	DatabaseControl DatabaseC;
	WeakReference<AddNewUserActivity> NewUserPage;

	public AddNewUserTask(DatabaseControl Data, AddNewUserActivity activity)
	{
		DatabaseC = Data;
		NewUserPage = new WeakReference<AddNewUserActivity>(activity);
	}

	@Override
	protected Boolean doInBackground(Void... voids)
	{
		User NewUser = new User();
		AddNewUserActivity Page = NewUserPage.get();
		NewUser.setLogin(Page.EditLoginName.getText().toString(),Page.EditPassword.getText().toString());
		NewUser.setValues(
			Page.EditFirstName.getText().toString(),
			Page.EditMiddleName.getText().toString(),
			Page.EditLastName.getText().toString(),
			Page.EditPhoneNumber.getText().toString(),
			Page.EditAddressLine.getText().toString(),
			Page.EditCity.getText().toString(),
			Page.EditProvince.getText().toString(),
			Page.EditCountry.getText().toString(),
			Page.EditPostalCode.getText().toString(),
			Page.EditEmergencyContact.getText().toString(),
			Page.EditEmergencyPhoneNumber.getText().toString()
		);
		DatabaseC.addUser(NewUser);
		return true;
	}

	@Override
	protected void onPostExecute(Boolean result)
	{
		//Report user has been added
		MainApp.log("Added new user.");
	}
}
